/**
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jimm.protocols.icq.packet.received;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.util.Log;

import org.jimm.protocols.icq.Flap;
import org.jimm.protocols.icq.RawData;
import org.jimm.protocols.icq.Snac;
import org.jimm.protocols.icq.core.IReceivable;
import org.jimm.protocols.icq.core.OscarConnection;
import org.jimm.protocols.icq.exceptions.BadPacketException;
import org.jimm.protocols.icq.request.Request;
import org.jimm.protocols.icq.request.RequestKeeper;
import org.jimm.protocols.icq.request.event.RequestAnswerEvent;

/**
 * <p>
 * Created by
 * 
 * @author Fabrice Michellonet
 * @author Samolisov Pavel
 */
public class ReceivedPacket extends Flap implements IReceivable {

	private static final String LOG_TAG = "ICQ:ReceivedPacket";

	public ReceivedPacket(DataInputStream strm, boolean hasSnac)
			throws IOException, BadPacketException {
		super();

		byte id = strm.readByte();
		if (id != FLAP_ID) {
			Log.d(LOG_TAG, "Bad packet (incorrect flap id " + id + ")");
			throw new BadPacketException();
		}

		setChannelId(strm.readByte());
		setSequenceNumber(strm.readUnsignedShort());
		int dataSize = strm.readUnsignedShort();

		if (hasSnac)
			addSnac(new Snac(strm, dataSize));
		else {
			// FIXME
			byte[] buf = new byte[dataSize];
			strm.read(buf);
			RawData data = new RawData(buf);
			addRawDataToFlap(data);
		}
	}

	/**
	 * This cause the callback of RequestListener classes, which have registered
	 * a requestId that match the requestId of the current packet.
	 * 
	 * @param connection
	 *            An active connection with any Oscar server.
	 */
	public void matchRequest(OscarConnection connection) {
		RequestKeeper requestKeeper = connection.getRequestKeeper();
		if (requestKeeper.containsRequest(this.getSnac().getRequestId())) {
			Request request = requestKeeper.getRequest(this.getSnac()
					.getRequestId());
			for (int i = 0; i < request.getNbListeners(); i++) {
				try {
					request.getRequestListener(i).onRequestAnswer(
							new RequestAnswerEvent(request.getMonitoredFlap(),
									this));
				} catch (UnsupportedEncodingException e) {
					Log.e(LOG_TAG, "Unsupported encoding exception", e);
				}
			}
			request.removeAllListener();
		}
	}

	public void execute(OscarConnection connection) throws Exception {
	};

	public void notifyEvent(OscarConnection connection) {
	};
}